<?php
require_once 'game.php';
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>狼人杀游戏</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <h1>狼人杀游戏</h1>
    <p>当前阶段: <?php echo $game['phase']; ?></p>
    <p>当前行动: <?php echo $game['current_action']; ?></p>

    <?php if ($gameOver): ?>
        <p>游戏结束！</p>
        <?php
        $wolfCount = 0;
        $villagerCount = 0;
        foreach ($game['players'] as $index => $role) {
            if ($game['alive'][$index]) {
                if ($role === '狼人') {
                    $wolfCount++;
                } else {
                    $villagerCount++;
                }
            }
        }
        $winner = ($wolfCount > 0) ? '狼人' : '村民';
        echo "<p>获胜方: $winner</p>";
        ?>
        <a href="?reset=1">重新开始</a>
    <?php else: ?>
        <h2>存活玩家</h2>
        <ul>
            <?php
            foreach ($game['players'] as $index => $role) {
                if ($game['alive'][$index]) {
                    echo "<li>$role (玩家 $index)</li>";
                }
            }
            ?>
        </ul>

        <?php if ($game['current_action'] === '狼人行动'): ?>
            <h2>狼人请选择要击杀的玩家</h2>
            <?php
            foreach ($game['players'] as $index => $role) {
                if ($game['alive'][$index]) {
                    echo "<button onclick=\"window.location.href='?action=wolf_kill&target=$index'\">玩家 $index</button>";
                }
            }
            ?>
        <?php elseif ($game['current_action'] === '预言家行动'): ?>
            <h2>预言家请选择要查验的玩家</h2>
            <?php
            foreach ($game['players'] as $index => $role) {
                if ($game['alive'][$index]) {
                    echo "<button onclick=\"window.location.href='?action=seer_check&target=$index'\">玩家 $index</button>";
                }
            }
            ?>
        <?php elseif ($game['current_action'] === '女巫行动'): ?>
            <h2>女巫行动</h2>
            <button onclick="window.location.href='?action=witch_heal'">使用解药</button>
            <?php
            foreach ($game['players'] as $index => $role) {
                if ($game['alive'][$index]) {
                    echo "<button onclick=\"window.location.href='?action=witch_poison&target=$index'\">对玩家 $index 使用毒药</button>";
                }
            }
            ?>
        <?php elseif ($game['current_action'] === '白天投票'): ?>
            <h2>白天投票，请选择要投票出局的玩家</h2>
            <?php
            foreach ($game['players'] as $index => $role) {
                if ($game['alive'][$index]) {
                    echo "<button onclick=\"window.location.href='?action=vote&target=$index'\">玩家 $index</button>";
                }
            }
            ?>
        <?php endif; ?>
    <?php endif; ?>

    <?php
    if (isset($_GET['reset'])) {
        session_destroy();
        header('Location: index.php');
        exit;
    }
    ?>
</body>

</html>